package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.SchoolHoliday;

public class SchoolHolidayRowMapper implements RowMapper<SchoolHoliday>{
    private int type;
    public static final int AND_DATESTR = 1;

    public SchoolHolidayRowMapper() {
    }

    public SchoolHolidayRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolHoliday mapRow(ResultSet rs, int rowNum) throws SQLException {
        SchoolHoliday sc = null;
        switch (type) {
            case AND_DATESTR:
                sc = parseSchoolHoliday(new SchoolHoliday(), rs, "sh");
                sc.setSdate_str(SystemDateFormatUtil.formatDate(sc.getSdate(), SystemDateFormatUtil.FORMAT_DATE));
                sc.setEdate_str(SystemDateFormatUtil.formatDate(sc.getEdate(), SystemDateFormatUtil.FORMAT_DATE));
                break;
            default:
                sc = parseSchoolHoliday(new SchoolHoliday(), rs);
        }
        return sc;
    }

    public static SchoolHoliday parseSchoolHoliday(SchoolHoliday s, ResultSet rs) throws SQLException {
        return parseSchoolHoliday(s, rs, null);
    }

    public static SchoolHoliday parseSchoolHoliday(SchoolHoliday s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setHid(rs.getLong(su+"hid"));
        } catch (SQLException e) {
            s.setHid(null);
        }
        try {
            s.setSdate(rs.getDate(su+"sdate"));
        } catch (SQLException e) {
            s.setSdate(null);
        }
        try {
            s.setEdate(rs.getDate(su+"edate"));
        } catch (SQLException e) {
            s.setEdate(null);
        }
        try {
            s.setDesinfo(rs.getString(su+"desinfo"));
        } catch (SQLException e) {
            s.setDesinfo(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolHoliday s) {
        par.put("sid", s.getSid());
        par.put("edate",s.getEdate());
        par.put("sdate",s.getSdate());
        par.put("hid", s.getHid());
        par.put("desinfo", s.getDesinfo());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolHoliday s) {
        args.put("hid",s.getHid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolHoliday s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "hid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolHoliday s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_holiday(");
        int count = 0;
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }

        if (null !=s.getEdate()) {
            sql.append("edate,");
            count++;
        }
        if (null !=s.getSdate()) {
            sql.append("sdate,");
            count++;
        }
        if (null !=s.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolHoliday s)
        throws SQLException {
        int count = 1;
        if (null != s.getSid()) {
            ps.setLong(count++,s.getSid());
        }
        if (null != s.getEdate()) {
            ps.setDate(count++, (Date) s.getEdate());
        }
        if (null != s.getSdate()) {
            ps.setDate(count++, (Date) s.getSdate());
    
        }
        if (null != s.getDesinfo()) {
            ps.setString(count++, s.getDesinfo());
    
        }

    }
}
